package zhoug.common.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;

import zhoug.common.prodivers.AppContextProvider;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;

/**
 * 发送Notification通知工具,单例模式
 * @Author 35574
 * @Date 2020/7/16
 * @Description
 */
public class NotificationHelper {
    private static final String TAG = ">>>NotificationUtils";
    private static NotificationHelper instance;
    private Context _appContext;
    private NotificationManager _manager;

    private NotificationHelper() {
        _manager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
    }

    /**
     * 获取单例
     * @return NotificationHelper
     */
    public static NotificationHelper getInstance() {
        if (instance == null) {
            synchronized (NotificationHelper.class) {
                if (instance == null) {
                    instance = new NotificationHelper();
                }
            }
        }
        return instance;
    }

    /**
     * 获取AppContext
     * @return
     */
    private Context getContext() {
        if (_appContext == null) {
            _appContext = AppContextProvider.getAppContext();
        }
        return _appContext;
    }

    /**
     * 获取NotificationManager
     * @return NotificationManager
     */
    public NotificationManager getManager() {
        if (_manager == null) {
            _manager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return _manager;
    }

    /**
     * 初始化默认的通知渠道
     * channelId:{@link Context#getPackageName()}
     * channelName:{@link Context#getPackageName()}
     */
    public void initDefaultChannel() {
        initChannel(null, null, true, false, true, null);
    }

    /**
     * 初始化通知渠道
     * 在Application中调用一次即可
     * 可以设置多个NotificationChannel 发送通知时用channelId区分
     * 同一个id的NotificationChannel 只需设置一次,多次设置后面的不生效,需要卸载了重新安装app
     *
     * @param channelId   频道的id。每个包必须是唯一的,
     * @param channelName 不同的NotificationChannel 可以相同的名字
     * @param lights      是否闪灯
     * @param vibration   是否震动
     * @param sound       是否声音
     * @param soundUri    声音的Uri 默认值{@link RingtoneManager#getDefaultUri(int)}
     *                    RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
     */
    public void initChannel(String channelId, String channelName, boolean lights, boolean vibration, boolean sound, Uri soundUri) {
        if (channelId == null) {
            channelId = getContext().getPackageName();
        }
        if (channelName == null) {
            channelName = getContext().getPackageName();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(channelId, channelName,
                    NotificationManager.IMPORTANCE_HIGH);//设置为LOW会没有声音 ;MAX：最重要的程度， 会弹出一个单独消息框，让用户做出相应
            mChannel.enableLights(lights);//设置闪灯
//            mChannel.setLightColor(Color.RED);//通知灯颜色,前提是该频道上的指示灯已启用,并且设备支持该颜色功能
            mChannel.enableVibration(vibration);//震动
            //声音
            if (sound) {
                if (soundUri == null) {
                    soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                }
                mChannel.setSound(soundUri, Notification.AUDIO_ATTRIBUTES_DEFAULT);
            }
            getManager().createNotificationChannel(mChannel);
        }
    }

    /**
     * NotificationCompat.Builder
     * @param context
     * @param channelId 渠道id 默认{@link Context#getPackageName()}
     * @return NotificationCompat.Builder
     */
    public NotificationCompat.Builder createBuild(Context context, String channelId) {
        if (channelId == null) {
            channelId = context.getPackageName();
        }
        return new NotificationCompat.Builder(context, channelId);

    }

    /**
     * 封装的最简单的通知发送.需要自定义通知请使用{@link #createBuild(Context, String)}
     * @param context
     * @param channelId 渠道id,必须是{@link #initChannel(String, String, boolean, boolean, boolean, Uri)}中设置的id
     * @param id 通知的id相同的id为同一个通知
     * @param ticker  通知没拉下来时状态栏显示的提示,不一定显示
     * @param contentTitle 内容的标题 拉下来后
     * @param content 内容
     * @param smallIcon 小图标
     * @param largeIcon 大图标,显示在通知内容后面
     * @param pendingIntent 点击通知跳转的页面 例:{@link PendingIntent#getActivity(Context, int, Intent, int)}
     */
    public void sendNotification(@NonNull Context context,  String channelId, int id, String ticker,
                                 String contentTitle, String content, int smallIcon, int largeIcon,
                                 PendingIntent pendingIntent) {
        if(channelId==null){
            channelId=context.getPackageName();
        }
        NotificationCompat.Builder builder = createBuild(context, channelId);
        builder.setAutoCancel(true);//点击跳转到pendingIntent后消失
        if (ticker != null) {
            builder.setTicker(ticker);//通知没拉下来时状态栏显示的提示,不一定显示
        }
        if (contentTitle != null) {
            builder.setContentTitle(contentTitle);// 内容的标题 拉下来后
        }
        if (content != null) {
            builder.setContentText(content);//设置内容
        }
        if (smallIcon != 0) {
            builder.setSmallIcon(smallIcon);//设置小图标
        }
        if (largeIcon != 0) {
            //设置大图标,显示在通知内容后面
            builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),largeIcon));
        }
        //设置点击通知跳转的页面
        if (pendingIntent != null) {
            builder.setContentIntent(pendingIntent);
        }
        Notification notification = builder.build();
        getManager().notify(id, notification);//发送通知
    }

}
